<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="queryParams.courseName" placeholder="请输入课程名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人" prop="responsiblePerson">
        <el-input v-model="queryParams.responsiblePerson" placeholder="请输入负责人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="教工号" prop="employeeNumber">
        <el-input v-model="queryParams.employeeNumber" placeholder="请输入教工号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程级别" prop="courseLevel">
        <el-select v-model="queryParams.courseLevel" placeholder="请选择课程级别" clearable>
          <el-option v-for="dict in dict.type.course_level" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="评选单位" prop="evaluationUnit">
        <el-input v-model="queryParams.evaluationUnit" placeholder="请输入评选单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="立项名称" prop="onlineCourseName">
        <el-input v-model="queryParams.onlineCourseName" placeholder="请输入立项名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="立项时间" prop="projectTime">
        <el-date-picker clearable v-model="queryParams.projectTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择立项时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="认定名称" prop="firstClassName">
        <el-input v-model="queryParams.firstClassName" placeholder="请输入认定名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="认定时间">
        <el-date-picker v-model="daterangeRecognitionTime" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="课程团队成员" prop="teamMembers">
        <el-input v-model="queryParams.teamMembers" placeholder="请输入课程团队成员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['courses:courses:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['courses:courses:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['courses:courses:remove']">删除</el-button>
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-check" size="mini" @click="openAuditDialog" :disabled="multiple"
          v-hasPermi="['system:check']" plain>审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['courses:courses:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 确保visible是通过sync双向绑定 -->
    <!-- 审核   -->
    <audit-dialog :visible.sync="auditDialogVisible" table_name="jw_courses" id_name="course_id" ref="audit"
      @submit="handleAuditSubmit" />

    <el-table v-loading="loading" :data="coursesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="courseId" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="负责人" align="center" prop="responsiblePerson" />
      <el-table-column label="教工号" align="center" prop="employeeNumber" />
      <el-table-column label="课程级别" align="center" prop="courseLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_level" :value="scope.row.courseLevel" />
        </template>
      </el-table-column>
      <el-table-column label="评选单位" align="center" prop="evaluationUnit" />
      <el-table-column label="立项名称" align="center" prop="onlineCourseName" />
      <el-table-column label="立项时间" align="center" prop="projectTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="认定名称" align="center" prop="firstClassName" />
      <el-table-column label="认定时间" align="center" prop="recognitionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recognitionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程团队成员" align="center" prop="teamMembers" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="未通过原因" align="center" prop="rejectionReason" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佐证材料" align="center" prop="wdUplocal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['courses:courses:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['courses:courses:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改课程情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1040px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="负责人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="教工号" prop="employeeNumber">
          <el-input v-model="form.employeeNumber" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="课程级别" prop="courseLevel">
          <el-select v-model="form.courseLevel" placeholder="请选择课程级别">
            <el-option v-for="dict in dict.type.course_level" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评选单位" prop="evaluationUnit">
          <el-input v-model="form.evaluationUnit" placeholder="请输入评选单位" />
        </el-form-item>
        <el-form-item label="立项名称" prop="onlineCourseName">
          <el-input v-model="form.onlineCourseName" placeholder="请输入立项名称" />
        </el-form-item>
        <el-form-item label="立项时间" prop="projectTime">
          <el-date-picker clearable v-model="form.projectTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择立项时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="认定名称" prop="firstClassName">
          <el-input v-model="form.firstClassName" placeholder="请输入认定名称" />
        </el-form-item>
        <el-form-item label="认定时间" prop="recognitionTime">
          <el-date-picker clearable v-model="form.recognitionTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择认定时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="课程团队成员" prop="teamMembers">
          <el-input v-model="form.teamMembers" placeholder="请输入课程团队成员" />
        </el-form-item>
        <el-form-item label="佐证材料" prop="wdUplocal">
          <file-upload v-model="form.wdUplocal" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourses, getCourses, delCourses, addCourses, updateCourses } from "@/api/courses/courses";
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue'; // 引入审核子组件

export default {
  name: "Courses",
  dicts: ['audit_status', 'course_level'],
  components: { AuditDialog },  // 注册审核组件
  data() {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程情况表格数据
      coursesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 佐证材料时间范围
      daterangeRecognitionTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        responsiblePerson: null,
        employeeNumber: null,
        courseLevel: null,
        evaluationUnit: null,
        onlineCourseName: null,
        projectTime: null,
        firstClassName: null,
        recognitionTime: null,
        teamMembers: null,
        auditStatus: null,
        wdUplocal: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        responsiblePerson: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        employeeNumber: [
          { required: true, message: "教工号不能为空", trigger: "blur" }
        ],
        courseLevel: [
          { required: true, message: "课程级别不能为空", trigger: "change" }
        ],
        evaluationUnit: [
          { required: true, message: "评选单位不能为空", trigger: "blur" }
        ],
        onlineCourseName: [
          { required: true, message: "立项名称不能为空", trigger: "blur" }
        ],
        projectTime: [
          { required: true, message: "立项时间不能为空", trigger: "blur" }
        ],
        firstClassName: [
          { required: true, message: "认定名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开审核对话框
    openAuditDialog() {
      if(this.ids!=null)
      {
        this.auditDialogVisible = true; // 显示审核对话框
        this.$refs['audit'].setData(this.ids);
      }
      else
        this.$message.error("请选择要审核的信息");
    },
    // 提交审核数据
    async handleAuditSubmit() {
      this.auditDialogVisible = false; // 关闭审核对话框
      await this.getList(); // 刷新数据列表
    },
    /** 查询课程情况列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRecognitionTime && '' != this.daterangeRecognitionTime) {
        this.queryParams.params["beginRecognitionTime"] = this.daterangeRecognitionTime[0];
        this.queryParams.params["endRecognitionTime"] = this.daterangeRecognitionTime[1];
      }
      listCourses(this.queryParams).then(response => {
        this.coursesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        responsiblePerson: null,
        employeeNumber: null,
        courseLevel: null,
        evaluationUnit: null,
        onlineCourseName: null,
        projectTime: null,
        firstClassName: null,
        recognitionTime: null,
        teamMembers: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
        updateTime: null,
        wdUplocal: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRecognitionTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程情况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourses(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除课程情况编号为"' + courseIds + '"的数据项？').then(function() {
        return delCourses(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('courses/courses/export', {
        ...this.queryParams
      }, `courses_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
